﻿<?php
	class page 
	{
	 /*****
	 	@$limitNumber 每页条数
		@$initPage 初始第几页
		@$currentPage 当前第几页
		@$sumPages 总共多少页
		@$showNumber 分页显示多少个
		@$url 传值url
		@$pageArray 分页数组
	*/

		private $limitNumber;
		private  $currentPage;
		private  $sumPages;
		private  $showNumber;
		private  $url;
		private  $start;
		private  $end;
		public  $pageArray = array();

		function __construct ($limitNumber,$sumNumber,$currentPage,$url,$showNumber){
			$this->limitNumber = intval($limitNumber);
			$this->sumPages = ceil(intval($sumNumber)/intval($limitNumber));
			$this->currentPage = intval($currentPage);
			$this->url = $url;
			$this->showNumber = intval($showNumber);
			$this->initPageArray();
			//$this->showNumberPage();
		}
		function __destruct()
		{
			unset($this->currentPage);
			unset($this->limitNumber);
			unset($this->sumPages);
			unset($this->url);
			unset($this->showNumber);
			unset($this->pageArray);
			unset($this->start);
			unset($this->end);
		}

		public function initPageArray()
		{

			for($i=1;$i<=$this->sumPages;$i++)
			{
				$this->pageArray[$i] = $i;
				// echo $this->pageArray[$i];
			}
		}

		public function showNumberPage()
		{
			(String) $showPageString='<div class="pageCss">';
			if($this->sumPages<=$this->showNumber)
			{
				if($this->currentPage<=1)
				{
					$showPageString .= '<div class="pageCssUnclick"><span>首页</span></div>';
					$showPageString .= '<div class="pageCssUnclick"><span>《</span></div>';
				}else{
					$showPageString .= '<div class="pageCssUnclick"><a href="'.$this->url.'?currentPage=1">首页</span></div>';
					$showPageString .= '<div class="pageCssUnclick"><a href="'.$this->url.'?currentPage='.($this->currentPage-1).'>《</a></div>';
				}

				for($i=1;$i<=$this->sumPages;$i++)
					{	
						if($this->currentPage==$i)
						{
							$showPageString .= '<div class="pageCssClick"><span>'.$this->pageArray[$i].'</span></div>';
						}else{	/*echo $this->pageArray[$i];*/
							$showPageString .= '<div class="pageCssUnclick"><a href="'.$this->url.'?currentPage='.$this->pageArray[$i].'">'.$this->pageArray[$i].'</a></div>';
						}

					}
				if($this->currentPage<$this->sumPages )
				{
					$showPageString .= '<div class="pageCssUnclick"><a href="'.$this->url.'?currentPage='.($this->currentPage+1).'"">》</a></div>';
					$showPageString .= '<div class="pageCssUnclick"><a href="'.$this->url.'?currentPage='.$this->pageArray[$this->sumPages].'"">尾页</a></div>';
				}else{
					$showPageString .= '<div class="pageCssUnclick"><span>》</span></div>';
					$showPageString .= '<div class="pageCssUnclick"><span>尾页</span></div>';
				}
			}else{
				if($this->currentPage<=1)
				{
					$showPageString .= '<div class="pageCssUnclick"><span>首页</span></div>';
					$showPageString .= '<div class="pageCssUnclick"><span>《</span></div>';
				}else {
					$showPageString .= '<div class="pageCssUnclick"><a href="'.$this->url.'?currentPage=1">首页</span></div>';
					$showPageString .= '<div class="pageCssUnclick"><a href="'.$this->url.'?currentPage='.($this->currentPage-1).'"">《</a></div>';
				}
				$this->start = $this->currentPage-floor($this->showNumber/2);
				$this->end = $this->currentPage+floor($this->showNumber/2);
				if($this->end >$this->sumPages	||	$this->currentPage>$this->sumPages-floor($this->showNumber/2))
				{
					$this->end = $this->sumPages;
					$this->start = $this->sumPages-2*floor($this->showNumber/2);
				}
				if($this->start<1)
				{
					$this->start = 1;
					$this->end = $this->showNumber;
				}
				//echo 'start'.$this->start;
				for($i=$this->start;$i<=$this->end;$i++)
					{
						if($this->currentPage==$i)
						{
							$showPageString .= '<div class="pageCssClick"><span>'.$this->pageArray[$i].'</span></div>';
						}else{
							$showPageString .= '<div class="pageCssUnclick"><a href="'.$this->url.'?currentPage='.$this->pageArray[$i].'">'.$this->pageArray[$i].'</a></div>';
						}

					}
				if($this->currentPage<$this->sumPages )
				{
					$showPageString .= '<div class="pageCssUnclick"><a href="'.$this->url.'?currentPage='.($this->currentPage+1).'">》</a></div>';
					$showPageString .= '<div class="pageCssUnclick"><a href="'.$this->url.'?currentPage='.$this->pageArray[$this->sumPages].'">尾页</a></div>';
				}else{
					$showPageString .= '<div class="pageCssUnclick"><span>》</span></div>';
					$showPageString .= '<div class="pageCssUnclick"><span>尾页</span></div>';
				}	
			}

				$showPageString .='</div>';
			return $showPageString;
		}
	}
?>